package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingPageVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Description NursingProjectServiceImpl
 * @Author wangwei
 * @Date 2024-08-23
 */
@Component
@Slf4j
public class NursingProjectServiceImpl implements NursingProjectService {
    @Autowired
    private NursingProjectMapper nursingProjectMapper;
    /**
     *分页查询护理项目
     * @param name
     * @param pageNum
     * @param pageSize
     * @param status
     * @return
     */
    @Override
    public NursingPageVo<NursingProject> getNursingProjectInfo(String name, Integer pageNum, Integer pageSize, Integer status) {
        log.info("接收请求参数为{},{},{},{}",name,pageNum,pageSize,status);
        //调用分页插件
        PageHelper.startPage(pageNum,pageSize);
        //查询护理项目集合信息
        Page<NursingProject> page = nursingProjectMapper.getNursingProjectInfo(name,status);
        //将护理项目封装成NursingProjectVo
        NursingPageVo<NursingProject> pageVo = new NursingPageVo<>(page);
        pageVo.setRecords(page.getResult());
        pageVo.getRecords().stream().forEach(info->{
            if (info.getCount()==null){
                info.setCount(0);
            }
        });
        log.info("返回信息为{}",pageVo);
        return pageVo;
    }

    /**
     * 添加项目功能
     * @param nursingProjectDto
     * @return
     */
    @Override
    public void addNursingProject(NursingProjectDto nursingProjectDto) {
        //1.数据装换
        NursingProject nursingProject = BeanUtil.toBean(nursingProjectDto, NursingProject.class);
        //2.补全信息
        nursingProject.setCreateTime(LocalDateTime.now());
        nursingProject.setUpdateTime(LocalDateTime.now());
        nursingProject.setCreateBy(12L);
        nursingProject.setCreator("测试");
        //3.业务逻辑
        //4.调用mapper
        nursingProjectMapper.addNursingProject(nursingProject);
    }

    /**
     * 根据ID获取护理项目
     * @param id
     * @return
     */
    @Override
    public NursingProject getProjectInfoByID(Integer id) {
        NursingProject nursingProject = nursingProjectMapper.getProjectInfoByID(id);
        return nursingProject;
    }

    /**
     * 更新护理项目信息
     * @param nursingProjectDto
     * @return
     */
    @Override
    public void updateProjectInfo(NursingProjectDto nursingProjectDto) {
        //1.切换数据类型
        NursingProject nursingProject = BeanUtil.toBean(nursingProjectDto, NursingProject.class);
        //2.补全信息
        nursingProject.setUpdateTime(LocalDateTime.now());
        nursingProject.setUpdater("测试");
        nursingProject.setUpdateBy(1L);
        //3.业务 逻辑
        //4.mapper
        nursingProjectMapper.updateProjectInfo(nursingProject);
    }

    /**
     * 根据ID修改护理项目的状态
     * @param id
     * @param status
     * @return
     */
    @Override
    public void updateStatus(Long id, Integer status) {
        nursingProjectMapper.updateStatus( id,  status);
    }

    /**
     * 根据ID删除护理项目
     * @param id
     * @return
     */
    @Override
    public void deleteProjectByID(Long id) {
        nursingProjectMapper.deleteProjectByID( id);
    }

    /**
     * 获取所有的护理项目信息
     * @return
     */
    @Override
    public List<NursingProjectDto> getAllProjectInfo() {
        return nursingProjectMapper.getAllProjectInfo();
    }
}
